var combinationSum2 = function(candidates, target) {

  // if (candidates.length === 0) return [];
  const ans = [], path = [];
  candidates.sort((a, b) => a - b);
  backtracking(0, 0, target, candidates, ans, path);
  return ans;

};

function backtracking(startIndex, sum, target, candidates, ans, path) {
  if (sum === target) {
    ans.push([...path]);
    return;
  }

  for (let i = startIndex; i < candidates.length; i++) {
    if (sum + candidates[i] > target) break;
    // 要对同一树层使用过的元素进行跳过
    if (i > startIndex && candidates[i] === candidates[i - 1]) continue;
    path.push(candidates[i]);
    backtracking(i + 1, sum + candidates[i], target, candidates, ans, path);
    path.pop();
  }
}


const candidates = [2,2,2], target = 2;
console.log(combinationSum2(candidates, target))